<template>
	<view class="richtext_container">
		<rich-text :nodes="content"></rich-text>
	</view>
</template>

<script>
	import api from '@/service/config/api.js'
	import checkTokenMixin from '@/mixins/check_token.js'
	export default {
		mixins: [checkTokenMixin],
		data() {
			return {
				key: '',
				id: '',
				dataInfo: {},
				value: '',
				imgList: [],
			}
		},
		computed: {
			content() {
				const data = this.value||this.dataInfo.valueStr||this.dataInfo||'';
				if(!data.replace) return ''
				return data.replace(/style=\"\"/gi,'').replace(/\<img/gi, '<img style="max-width:100%";height:auto');
			}
		},
		onLoad(options) {
			if (!options.key && !options.id) {
				return this.$utils.showToast('未找到该页面', 1500, 1)
			}
			if (options.key) {
				this.key = options.key
				return this.getKeyContent()
			}
			if (options.id) {
				this.id = options.id
				if (options.type == 'news') {
					return this.getNewsContent()
				}
				if (options.banner) {
					return this.getBannerContent()
				}
				return this.getIdContent()
			}
		},
		methods: {
			getTitile(data) {
				switch (data) {
					case 'consumer_agreement':
						return '用户协议';
					case 'private_agreement':
						return '隐私政策';
					case 'about_us':
						return '关于我们';
					case 'invitation_rules':
						return '邀请规则';
					default:
						return data
				}
			},
			getKeyContent() {
				this.$utils.loading()
				api.get(this.checkToken?'/base/api/tAppletProtocol/type':'/base/api/tAppletProtocol/inner/type', {
					key: this.key,
					tenantId: uni.getStorageSync("tenantId")
				}).then(res => {
					this.dataInfo = res.data
					if (res.data && res.data.keyStr) {
						uni.setNavigationBarTitle({
							title: this.getTitile(res.data.keyStr)
						})
					}
				})
			},
			getIdContent() {
				this.$utils.loading()
				api.get('/base/api/tAppletProtocol/' + this.id).then(res => {
					this.dataInfo = res.data
					if (res.data && resres.data.keyStr) {
						uni.setNavigationBarTitle({
							title: this.getTitile(res.data.keyStr)
						})
					}
				})
			},
			getBannerContent() {
				this.$utils.loading()
				api.get('/base/api/tAppletBanner/' + this.id).then(res => {
					this.value = res.data.productId
					if (res.data.name) {
						uni.setNavigationBarTitle({
							title: res.data.name
						})
					}
				})
			},
			getNewsContent() {
				this.$utils.loading()
				api.get('/user/tMyNotice/' + this.id).then(res => {
					const content = res.data.content ? JSON.parse(res.data.content) : {}
					this.value = content.detail || ''
					this.imgList = content.imgList ? content.imgList.split(',') : []
					if (res.data.title) {
						uni.setNavigationBarTitle({
							title: res.data.title
						})
					}
					api.get('/user/tMyNotice/read',{
						id: this.id
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	img {
		max-width: 100%;
	}
</style>